package com.cs.beneficios.web.action;
import com.cs.beneficios.business.entity.AtributosDAO;
import com.cs.beneficios.business.entity.SubProgramaDAO;
import com.cs.beneficios.business.model.SubProgramaVO;
//import com.cs.beneficios.business.model.Subprograma;
//import com.cs.beneficios.business.model.ValorFlexibleVO;
import com.cs.beneficios.common.ConnectionFactory;
import com.cs.beneficios.common.entity.Consultas;
import com.cs.paoyer.administration.business.model.UserVO;
import java.sql.SQLException;
import java.util.Collection;
import java.util.LinkedList;
import java.util.List;
import java.util.ArrayList;
import javax.servlet.http.HttpSession;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForward;
//import org.apache.struts.action.ActionError;
//import org.apache.struts.action.ActionErrors;
import java.io.IOException;
import javax.servlet.ServletException;

public class BuscarAtributoAction extends Action
{
  /**
   * This is the main action called from the Struts framework.
   * @param mapping The ActionMapping used to select this instance.
   * @param form The optional ActionForm bean for this request.
   * @param request The HTTP Request we are processing.
   * @param response The HTTP Response we are processing.
   * @return 
   * @throws java.io.IOException
   * @throws javax.servlet.ServletException
   */
  public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException
  {
  
    //ConnectionFactory cf = new ConnectionFactory("beneficios","beneficios");
	ConnectionFactory cf = new ConnectionFactory();
    Consultas objCons = new Consultas(cf);
    try{
        List lAtriSub = new LinkedList();
        SubProgramaVO objsub = new SubProgramaVO();
        HttpSession session = request.getSession();
        SubProgramaVO criterea = new SubProgramaVO();
        UserVO user = (UserVO)session.getAttribute("userInfo");  
        SubProgramaDAO objSubProgramaDAO = new SubProgramaDAO(cf);
        Collection lSubprog = objSubProgramaDAO.findSubProgramaByCriterea(criterea,user.getCode(),user.getUserGroupName(),user.getCodeArea());
        cf.closeConnection(objSubProgramaDAO.getConn());
        //Collection lSubprog = objCons.getSubprogramas();
        Collection ListaTamano = new ArrayList();
        
        SubProgramaVO objsubproVO = new SubProgramaVO();
        String codsubprograma = request.getParameter("codigo");
		if(codsubprograma == null){
			codsubprograma = (String)session.getAttribute("codigo");
			session.removeAttribute("codigo");
		}
        objsubproVO.setCode(new Long(codsubprograma));
        
        SubProgramaDAO objsubproDAO = new SubProgramaDAO(cf);
        boolean estado = objsubproDAO.getEstadoSubprograma(objsubproVO);
        
         if(estado){
            session.setAttribute("mensajes","El beneficio esta desactivado, no se pueden ver los atributos");
            return mapping.findForward("fail");
         }
         
		boolean sedeBeneficio = objsubproDAO.isRelatedWithSede(codsubprograma);
        
		if(!sedeBeneficio){
			String nameBeneficio = objsubproDAO.getNameByCode(codsubprograma);
			String codPrograma = objsubproDAO.getPrograma(codsubprograma);
			request.setAttribute("codSubprograma", codsubprograma);
			request.setAttribute("codPrograma", codPrograma);
			session.setAttribute("codigo", codsubprograma);
            request.setAttribute("msg", "El beneficio "+nameBeneficio+" no se encuentra seleccionado en la lista, debido a que no esta asociado a ninguna sede");
		}
        
        List lista = (List)lSubprog;
        
		if(lSubprog.size() > 0){
			objsub = (SubProgramaVO)lista.get(0);
			ListaTamano.add(objsub); 
			session.setAttribute("listatam",ListaTamano);
			session.setAttribute("subprogramas",lSubprog);
		} else {
			session.removeAttribute("listatam");
			session.removeAttribute("subprogramas");
			session.setAttribute("mensajes","Verifique si el usuario se encuentra asociado al beneficio");
			return mapping.findForward("fail");
		}

        String strTipo= request.getParameter("accion");
        
        if (strTipo!=null)
        {
           session.setAttribute("subpro",request.getParameter("codigo")); 
           int opcion =1;
           String strSubprograma = request.getParameter("codigo");
           if((Object)strSubprograma == null)
           {
             strSubprograma  =(String)session.getAttribute("codsubpro");
           }
           try{
             AtributosDAO atriDAO = new AtributosDAO(cf);
             int conversion = atriDAO.getAtributosVallista(strSubprograma);
             cf.closeConnection(atriDAO.getConn());
           }catch(SQLException e){
			System.err.println("/******** TDSUB - cwsub");
			e.printStackTrace();
			System.err.println("TDSSPE - cwsub ********/");
           }
           session.setAttribute("codsubpro",strSubprograma);
           
           lAtriSub = objCons.getAtributoEspecificos(strSubprograma,opcion);
                 
           request.setAttribute("tamanoatri",lAtriSub.size()+"");
           session.setAttribute("atrisubprog",lAtriSub);
           
           cf.closeConnection(objCons.getConn());
           cf.closeConnection(objsubproDAO.getConn());
           
           
        }
    }catch(SQLException e)
    {
		System.err.println("/******** TDSUB - cwsub");
		e.printStackTrace();
		System.err.println("TDSSPE - cwsub ********/");
    }finally
    {
      try{
        cf.closeConnection(objCons.getConn());
      }catch(SQLException e)
      {
		System.err.println("/******** TDSUB - cwsub");
		e.printStackTrace();
		System.err.println("TDSSPE - cwsub ********/");
      }
    }
    
    
    return mapping.findForward("success");
  }
}